<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html>
    <style type="text/css">
     .selectkpiClass{
	       height:23px;
	       line-height:23px;
		   width:90px;
		   border:#cbcbcb solid 1px;
		   margin-top:3px;
		   margin-right:5px;
		   margin-bottom:2px;
		   margin-left:2px;
     }
     input[type="text"]{
        height:23px;
        line-height:23px;
	    width:250px;
	    border:#cbcbcb solid 1px;
	    vertical-align: middle;
	    margin-top:3px;
     }
     .tableTd{
       margin-top:3px;
       text-align:right;
       width:100px;
       height:23px;
       vertical-align: middle;
       font-size:13px;
     }
    </style>
   
   
    <!-- 自定义javascript函数 -->
    <script type="text/javascript">
     //申请全局变量
     var kpiDialog=false;//标识是否启动弹窗口dialog
     var kpiList=new Array();
     var kpiDialogList=new Array();
     $(document).ready(function(){
     	var pars="userId="+$("#dqm_kpiSubscrible_userId").val()+"&resourceId="+$("#dqm_kpiSubscrible_page_subscribleId").val();
     	//初始化用户订阅信息
     	$.ajax({
			type : "post",
			async: false,
			url : '${pageContext.request.contextPath}/basemgr/kpiSubscribleAction!getSubscribleDefineByuserId.action',
			data:pars,
			dataType : "json",
			cache : true,
		    success : function(data){
				if(""!=data){
					//告警时
 				   if(data[0].warnSend=="1"){
 					   document.getElementById("dqm_kpiSubscrible_page_warnSend_radio_Id").checked=true;
 				   }else{
 					   document.getElementById("dqm_kpiSubscrible_page_warnSend_radio_Id").checked=false;
 				   }
 				   //周期性
 				   if(data[0].periodType=="3"){//日粒度
 				         $("#dqm_kpiSubscrible_page_select_dayormonth").val("3");
 				   
 				         $("#dqm_kpiSubscrible_page_datetime_day").my97({
			 	   	         dateFmt:'yyyyMMdd',
			 	   	         width:120,
			 		   	     startDate:[{
			 		   			 doubleCalendar:false,
			 		   			 isShowWeek:false,
			 		   			 isShowClear:false,
			 		   			 readOnly:true
			 		   	     }]
			 	        });
 				        $("#dqm_kpiSubscrible_page_datetime_day").val(data[0].startDataDate);
 				   }else if(data[0].periodType=="2"){//月粒度
 				          $("#dqm_kpiSubscrible_page_select_dayormonth").val("2");
	 				      $("#dqm_kpiSubscrible_page_datetime_day").my97({
				 	   	         dateFmt:'yyyyMM',
				 	   	         width:120,
				 		   	     startDate:[{
				 		   			 doubleCalendar:false,
				 		   			 isShowWeek:false,
				 		   			 isShowClear:false,
				 		   			 readOnly:true
				 		   	     }]
				 	        });
 				          $("#dqm_kpiSubscrible_page_datetime_day").val(data[0].startDataDate);
 				   }else{
 					  $("#dqm_kpiSubscrible_page_select_dayormonth").val("4");
 					  $("#dqm_kpiSubscrible_page_datetime_day").my97({
			 	   	         dateFmt:'yyyyMMddHH',
			 	   	         width:120,
			 		   	     startDate:[{
			 		   			 doubleCalendar:false,
			 		   			 isShowWeek:false,
			 		   			 isShowClear:false,
			 		   			 readOnly:true
			 		   	     }]
			 	        });
			          $("#dqm_kpiSubscrible_page_datetime_day").val(data[0].startDataDate);
 				   }
 				   for(var idx=0;idx<data.length;idx++){
 				      //短信
 				      if(data[idx].subscribeMode=="1"){
 				        document.getElementById("dqm_kpiSubscrible_page_checkbox_indicators_001").checked=true;
 				      }
 				      //邮件
 				      if(data[idx].subscribeMode=="2"){
 				        document.getElementById("dqm_kpiSubscrible_page_checkbox_indicators_002").checked=true;
 				      }
 				      //系统公告
 				      if(data[idx].subscribeMode=="3"){
 				         document.getElementById("dqm_kpiSubscrible_page_checkbox_indicators_003").checked=true;
 				      }
 				  }
		       }else{
		    	     $("#dqm_kpiSubscrible_page_select_dayormonth").val("3");
 				   
			         $("#dqm_kpiSubscrible_page_datetime_day").my97({
		 	   	         dateFmt:'yyyyMMdd',
		 	   	         width:120,
		 		   	     startDate:[{
		 		   			 doubleCalendar:false,
		 		   			 isShowWeek:false,
		 		   			 isShowClear:false,
		 		   			 readOnly:true
		 		   	     }]
		 	        });
			        $("#dqm_kpiSubscrible_page_datetime_day").val(getDateStr(0));
		       }
		    }
       });
     	
     	
     });
     //删除订阅指标
     function deleteKpiSubscrible(){
	      var pars="";
		  pars+="resourceId="+$("#dqm_kpiSubscrible_page_subscribleId").val()+"&userId="+$("#dqm_kpiSubscrible_page_userId").val();
		  //执行删除订阅指标操作
          $.ajax({
			 url : '${pageContext.request.contextPath}/basemgr/kpiSubscribleAction!deleteTdSubScribleDefineData.action',
			 data:pars,
			 dataType :'json',
			 cache : true,
			 success : function(r1){ 
			       $.messager.show({
						title:'指标订阅信息',
						msg:r1.returnMsg,
						showType:'slide'
					});
			 }
          });
		     
		  
     }
     /**
      *保存订阅指标
      *保存值模型： 员工@@订阅方式@@告警@@周期
      *李四@@1;2;3@@1@@cycle==0!!20120812 
      *李四@@1;2;3@@0@@cycle==1!!201208
      */
     function saveKpiSubscrible(){
    	  var pars="";
    	  var userId=$("#dqm_kpiSubscrible_page_userId").val();
    	      pars+=userId+"@@";
	      //获取订阅方式
	      var kpiSubscripteIndicators=getKpiSubscripteIndicators();
	      if(kpiSubscripteIndicators.length>0){
	    	  pars+=kpiSubscripteIndicators+"@@";
	      }else{
	    	  alert("请选择订阅方式!"); 
	      }
	      //获取发布频率
	      var releaseFrequency=getReleaseFrequency();
	      pars+=releaseFrequency;
    	  //执行订阅操作
             $.ajax({
				 url : '${pageContext.request.contextPath}/basemgr/kpiSubscribleAction!insertAndUpdateKpiSubscrible.action?kpiSubscribleList='+pars,
				 data:serializeObject($('#dqm_kpiSubscrible_page_kpisubscribe_form')),
				 dataType :'json',
				 cache : true,
				 success : function(r1){ 
				       $.messager.show({
							title:'指标订阅信息',
							msg:r1.returnMsg,
							showType:'slide'
						});
				 }
	       });  
     }
     //获取订阅方式数据
     function getKpiSubscripteIndicators(){
        var str="";
        $("#dqm_kpiSubscrible_page_checkbox_indicators").find($("input[type=checkbox]:checked")).each(function(){ //由于复选框一般选中的是多个,所以可以循环输出
             str+=$(this).val()+";";
        });
        return str.substring(0,str.length-1);
     }
     //获取发布方式数据
     function getReleaseFrequency(){
       var str="";
       var warnSend=null;//告警值
       if($("#dqm_kpiSubscrible_page_warnSend_radio_Id").attr("checked")!=undefined){
      	   str+="1@@";
       }else{
      	   str+="0@@";
       }
       var PeriodType=$("#dqm_kpiSubscrible_page_select_dayormonth").val();//获取周期类型
       var dataDate=$("#dqm_kpiSubscrible_page_datetime_day").val();//获取日期数据
       str+=PeriodType+"!!"+dataDate;
       return str;
     }
     //周期类型改变:弹出框
	   function ChangePeriodType(){
	 	  var value=$("#dqm_kpiSubscrible_page_select_dayormonth").val();
	 	   $("#dqm_kpiSubscrible_page_datetime_day").val("");//清空数据
	 	   if(value==3){//日
	 		   $("#dqm_kpiSubscrible_page_datetime_day").my97({
	 	   	         dateFmt:'yyyyMMdd',
	 	   	         width:120,
	 		   	     startDate:[{
	 		   			 doubleCalendar:false,
	 		   			 isShowWeek:false,
	 		   			 isShowClear:false,
	 		   			 readOnly:true
	 		   	     }]
	 	        });
	 	        $("#dqm_kpiSubscrible_page_datetime_day").val(getDateStr(0));
	 	   }else if(value==2){//月
	 		   $("#dqm_kpiSubscrible_page_datetime_day").my97({
	 	   	         dateFmt:'yyyyMM',
	 		   	     startDate:[{
	 		   			 doubleCalendar:false,
	 		   			 isShowWeek:false,
	 		   			 isShowClear:false,
	 		   			 readOnly:true
	 		   	     }]
	 	        });
	 		    $("#dqm_kpiSubscrible_page_datetime_day").val(myformatter(0));
	 	   }else{//时
	 		   $("#dqm_kpiSubscrible_page_datetime_day").my97({
	 	   	         dateFmt:'yyyyMMddHH',
	 		   	     startDate:[{
	 		   			 doubleCalendar:false,
	 		   			 isShowWeek:false,
	 		   			 isShowClear:false,
	 		   			 readOnly:true
	 		   	     }]
	 	        });
	 		   $("#dqm_kpiSubscrible_page_datetime_day").val(getHourStr());
	 	   }
	    }
	 //格式化日期函数:日
	   function getDateStr(AddDayCount) {
		    var dd = new Date();
		    dd.setDate(dd.getDate()+AddDayCount);//获取AddDayCount天后的日期
		    var y = dd.getFullYear();
		    var m = dd.getMonth()+1;//获取当前月份的日期
		    if(m<=9){
		    	m="0"+m;
		    }
		    var d = dd.getDate();
		    if(d<=9){
		    	d="0"+d;
		    }
		    return y+m+d;
	   }
	   //格式化日期函数:月
	   function myformatter(AddMonth){
	       var date=new Date();
	  		var y = date.getFullYear();
	  		var m ="";
	  		if(AddMonth==0){
	  			m = date.getMonth()+1;
	  		}else{
	  			m = date.getMonth();
	  		}
	  		var d = date.getDate();
	  		return y+(m<10?('0'+m):m);
	    }
	   //格式化日期函数:时
	   function getHourStr() {
		    var dd = new Date();
		    dd.setDate(dd.getDate());//获取AddDayCount天后的日期
		    var y = dd.getFullYear();
		    var m = dd.getMonth()+1;//获取当前月份的日期
		    if(m<=9){
		    	m="0"+m;
		    }
		    var h = dd.getHours();//获取当前小时
		    if(h<10){
		       h="0"+h;
		    }
		    var d = dd.getDate();
		    if(d<=9){
		    	d="0"+d;
		    }
		    return y+m+d+h;
	   }
	</script>
	 <input type="hidden" id="dqm_kpiSubscrible_userId" value="${sessionInfo.userid}"/>
     <form id="dqm_kpiSubscrible_page_kpisubscribe_form">
	   <table align="center" style="width:400px;margin-top:5px;">
	       <tr>
	          <td class="tableTd"><s:property value='#pTdKpiSubscrible.reporttypename' />编码:</td>
	          <td><input type="text" id="dqm_kpiSubscrible_page_subscribleId" name="resourceId" readonly="readonly" value="<s:property value='#pTdKpiSubscrible.resourceId'/>"></td>
	       </tr>
	        <tr>
	          <td class="tableTd"><s:property value='#pTdKpiSubscrible.reporttypename' />名称：</td>
	          <td ><input type="text" id="" name="subscribleName" readonly="readonly" value="<s:property value='#pTdKpiSubscrible.resourceName' />"></td>
	       </tr>
	        <tr>
	          <td class="tableTd">类型：</td>
	          <td>
	               <input type="text" id="" name="" readonly="readonly" value="<s:property value='#pTdKpiSubscrible.reporttypename' />">
	               <input type="hidden" id="" name="reporttype" value="<s:property value='#pTdKpiSubscrible.reporttype' />">
	          </td>
	       </tr>
	        <tr>
	          <td class="tableTd">订阅人：</td>
	          <td>
	              <input type="hidden" id="dqm_kpiSubscrible_page_userId" value="${sessionInfo.userid}" name="userId"/>
	              <input type="text" id="" name="userName" readonly="readonly" value="${sessionInfo.username}">
	          </td>
	       </tr>
	       <tr>
	          <td class="tableTd">开始推送时间：</td>
	          <td>
	             <table>
	                    <tr>
	                        <td>
	                           <select id="dqm_kpiSubscrible_page_select_dayormonth" style="margin:0px;margin-right:5px;margin-top:2px;" class="selectkpiClass" onchange="ChangePeriodType()">
	                                <option value="3" selected="selected">日</option>
									<option value="2">月</option>
									<option value="4">时</option>
	                           </select>
	                        </td>
	                        <td>
	                           <input id="dqm_kpiSubscrible_page_datetime_day"  type="date"  style="width:155px;margin-top:2px;"/>
	                        </td>
	                     </tr>
	             </table>
	          </td>
	        </tr>
	        <tr>
	          <td class="tableTd">订阅方式：</td>
	          <td>
	             <div id="dqm_kpiSubscrible_page_checkbox_indicators" style="border:#cbcbcb solid 1px;margin-top:3px;width:250px;height:23px;line-height:23px;">
	                <input type="checkbox" id="dqm_kpiSubscrible_page_checkbox_indicators_001" name="" value="1">短信&nbsp;&nbsp;
	                <input type="checkbox" id="dqm_kpiSubscrible_page_checkbox_indicators_002" name="" value="2">邮件&nbsp;&nbsp;
	                <input type="checkbox" id="dqm_kpiSubscrible_page_checkbox_indicators_003" name="" value="3">系统公告
	             </div>
	          </td>
	       </tr>
	        <tr>
	          <td class="tableTd">告警：</td>
	          <td>
		          <div id="" style="border:#cbcbcb solid 1px;vertical-align:middle;height:20px;line-height:20px;margin-top:3px;width:250px;">
		              &nbsp;<input type="checkbox" id="dqm_kpiSubscrible_page_warnSend_radio_Id" name=""/>&nbsp;告警时发送
		          </div>
	          </td>
	       </tr>
	       <tr>
	          <td colspan="2" >
	               <input type="button" style="margin-left:50px;margin-right:120px;margin-top:10px" class="btn_Class" value="订阅"    onclick="saveKpiSubscrible()"/>
	               <input type="button" style="margin-top:10px"  class="btn_Class" value="取消订阅" onclick="deleteKpiSubscrible()"/>
	          </td>
	       </tr>
	  </table>
	</form>